<%@page import="nl.hva.web.mvcwebapp.models.Course"%>
<%@include file="/WEB-INF/templates/configuration.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@page import="nl.hva.web.mvcwebapp.models.CourseUser"%>
<%
    HttpSession session_user = request.getSession(true);
    String firstname = (String) session_user.getAttribute("user_firstname");
    String user_username = (String) session_user.getAttribute("user_username");
    //int user_id = 0;
    int user_type = 0;
    if (firstname != null) {
        //user_id = Integer.valueOf("" + session_user.getAttribute("user_id"));
        user_type = Integer.valueOf("" + session_user.getAttribute("user_type"));
    }

%>
<h2>My Courses</h2>
<c:choose>
    <c:when test="${amountcourses != 0}">
        <!-- Wanneer er gebruikers opgeslagen zijn, worden ze hier getoond -->
        <table class="zebra">
            <thead>
                <tr>
                    <th style="width:25%;"> 
                        <strong>Name</strong>
                    </th>
                    <th style="width:50%;">
                        <strong>Description</strong>
                    </th>
                    <%if (user_type == 2 || user_type == 3) {
                    %>
                    <th style="width:15%;">
                        <strong>Controls</strong>
                    </th>
                    <%                        }%>
                </tr>
            </thead>
            <c:forEach var="tempCourse" items="${courses}">
                <!-- Per gebruiker wordt nu een rij aangemaakt met daarin zijn gegevens -->
                <tr>
                    <td>
                        <a href="${url}course/view?id=${tempCourse.pk.course.courseid}">${tempCourse.pk.course.coursetitle}</a>
                    </td>
                    <td>
                        ${tempCourse.pk.course.coursedescription}
                    </td>
                   <%
                        CourseUser tempcourse = (CourseUser) pageContext.getAttribute("tempCourse");
                        String headteacher = tempcourse.getCourse().getHeadteacher();

                        if (user_type == 2) {
                            System.out.println(headteacher+" "+user_username);
                            if (user_username.equals(headteacher)) {
                    %>
                    <td>
                        <a class="icon view" href="${url}course/view?id=${tempCourse.pk.course.courseid}">view</a>
                        <a class="icon edit" href="${url}course/edit?id=${tempCourse.pk.course.courseid}">edit</a>
                        <a class="icon delete" href="javascript:if(confirm('Weet u zeker dat u deze cursus wil verwijderen?'))
                           window.location='${url}course/delete?courseid=${tempCourse.pk.course.courseid}';">delete</a>
                    </td>
                    <%}
                        }%>
                    <%if (user_type == 3) {
                    %>
                    <td>
                        <a class="icon view" href="${url}course/view?id=${tempCourse.pk.course.courseid}">view</a>
                        <a class="icon edit" href="${url}course/edit?id=${tempCourse.pk.course.courseid}">edit</a>
                        <a class="icon delete" href="javascript:if(confirm('Weet u zeker dat u deze cursus wil verwijderen?'))
                           window.location='${url}course/delete?courseid=${tempCourse.pk.course.courseid}';">delete</a>
                    </td>
                    <%                       }%>
                </tr>
            </c:forEach>
        </table>
    </c:when>
    <c:otherwise>
        <!-- Als er geen cursussen zijn, wordt deze melding getoond -->
        There are no courses found.
    </c:otherwise>
</c:choose>

<%if (user_type == 2 || user_type == 3) {
%>
<p>
    <a class="button" href="${url}course/new">Add a new course</a>
</p>
<p>
    <a class="button" href="${url}index">Back to index</a>
</p>
<%                }%>
